import datetime
from applications.extensions import db
from sqlalchemy import UniqueConstraint






class VerifyRawYPModel(db.Model):
    __tablename__ = '原料_yp_请验'
    id = db.Column(db.Integer, primary_key=True, comment='ID') #默认自增primary_key
    verify_dh = db.Column(db.String(255), comment='单据编号',index=True, nullable=False)
    store_location_id = db.Column(db.Integer,db.ForeignKey("原料_yp_按货位入库.id"), comment='按货位入库_编号', nullable=False) # 属性 外键
    verify_number = db.Column(db.Numeric(16,4), comment='请验数量 保留小数点后4位',nullable=False)
    type = db.Column(db.String(255), default='请验单', comment='请验单')
    date_time = db.Column(db.Date, default=datetime.date.today(), comment='单据日期')
    user_verifyer_id = db.Column(db.Integer, db.ForeignKey("admin_user.id"), comment='请验人员_编号', nullable=False)
    verify_project=db.Column(db.String(255), default='全项检验', comment='请验项目：全项检验|部分检验')
    verify_department = db.Column(db.String(255), default='仓储部', comment='请验部门：仓储部|生产车间二')
    verify_type = db.Column(db.String(255), default='正常请验', comment='请验类型:正常请验|临期请验|小样请验')
    remark = db.Column(db.String(255), comment='备注')
    create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_at = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now,
                          comment='更新时间')

    store_location = db.relationship('StoreRawYPModel_location', backref=db.backref('verifyRawYp'),uselist=False)
    user_verifyer = db.relationship('User', foreign_keys=[user_verifyer_id], backref=db.backref('verifyRawYp_verifyer'),uselist=False)


    # #创建联合索引
    # __table_args__ = (
    #     UniqueConstraint('代码', '版本号', name='unix_raw_yp_code_version'),
    # )
    def to_dict(self):
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}

class VerifyRawYPModelSample(db.Model):
    __tablename__ = '原料_yp_请验_小样'
    id = db.Column(db.Integer, primary_key=True, comment='ID') #默认自增primary_key
    verify_dh = db.Column(db.String(255), comment='单据编号',index=True, nullable=False)
    产地 = db.Column(db.String(255), comment='产地', nullable=False)
    规格 = db.Column(db.String(255), comment='规格', nullable=False)
    批号 = db.Column(db.String(255), comment='批号', nullable=False)
    单位 = db.Column(db.String(255), comment='单位', nullable=False)
    raw_id = db.Column(db.Integer,db.ForeignKey("商品档案_原料_饮片.id"), comment='商品档案_编号', nullable=False) # 属性 外键
    verify_number = db.Column(db.Numeric(16,4), comment='请验数量 保留小数点后4位',nullable=False)
    type = db.Column(db.String(255), default='请验单', comment='请验单')
    date_time = db.Column(db.Date, default=datetime.date.today(), comment='单据日期')
    user_verifyer_id = db.Column(db.Integer, db.ForeignKey("admin_user.id"), comment='请验人员_编号', nullable=False)
    verify_project=db.Column(db.String(255), default='全项检验', comment='请验项目：全项检验|部分检验')
    verify_department = db.Column(db.String(255), default='仓储部', comment='请验部门：仓储部|生产车间二')
    verify_type = db.Column(db.String(255), default='正常请验', comment='请验类型:正常请验|临期请验|小样请验')
    remark = db.Column(db.String(255), comment='备注')
    create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_at = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now,
                          comment='更新时间')

    raw = db.relationship('RawYPModel', backref=db.backref('verifyRawYp_'),uselist=False) #uselist=False 标识一对一关系
    user_verifyer = db.relationship('User', foreign_keys=[user_verifyer_id], backref=db.backref('verifyRawYp_sample_verifyer'),uselist=False)



    def to_dict(self):
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}